//
//  JoUserEditViewController.m
//  RunningMan
//
//  Created by 姜勇利 on 16/5/23.
//  Copyright © 2016年 JohnnyB. All rights reserved.
//

#import "JoUserEditViewController.h"
#import "masonry.h"
#import "Privatism.h"
#import "EMSDk.h"
#import <BmobSDK/Bmob.h>

@interface JoUserEditViewController () <UITextFieldDelegate>

@property (nonatomic, strong) UITextField *edit;

@property (nonatomic, strong) NSArray *arrPrivatism;

@property (nonatomic, strong) UIView *sexView;

@end

@implementation JoUserEditViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    if (self.number != 3) {
       [self creatSubViews];
    }
    else
    {
        [self creatSexSubViews];
    }
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(handleSave:)];
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor grayColor];
    self.view.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];

}
- (void)handleSave:(UIBarButtonItem *)button
{
    if (self.navigationItem.rightBarButtonItem.tintColor == [UIColor grayColor]) {
        
    }
    else
    {
        BmobQuery *query = [BmobQuery queryWithClassName:@"userInfo"];
        [query whereKey:@"userName" equalTo:[EMClient sharedClient].currentUsername];
        [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
            for (BmobObject *obj in array) {
                        if (self.number == 2) {
                            [obj setObject:self.edit.text forKey:@"name"];
                        }
                        if (self.number == 5) {
                            [obj setObject:self.edit.text forKey:@"autogrph"];
                        }
                        [obj updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                            if (isSuccessful) {
                                NSLog(@"数据更新成功");
                                [self.navigationController popViewControllerAnimated:YES];
                            }
                            else
                            {
                                [self.navigationController popViewControllerAnimated:YES];
                                NSLog(@"%@", error);
                            }
                        }];
                    }
        }];
    }
}

#pragma mark -
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.edit becomeFirstResponder];
}

#pragma mark - 创建性别选择视图
- (void)creatSexSubViews
{
    self.sexView = [[UIView alloc]initWithFrame:CGRectMake(0, 80, self.view.frame.size.width, self.view.frame.size.height / 3.5f)];
    UIButton *maleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_sexView addSubview:maleButton];
    maleButton.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 10.5f);
    [maleButton setTitle:@"Male" forState:UIControlStateNormal];
    [maleButton addTarget:self action:@selector(handleSelectSex:) forControlEvents:UIControlEventTouchUpInside];
    maleButton.tag = 100;
    [maleButton setTitleColor:[UIColor colorWithRed:0.01 green:0.66 blue:0.62 alpha:1.0] forState:UIControlStateNormal];
    
    UIButton *femaleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    femaleButton.frame = CGRectMake(0, self.view.frame.size.height / 10.5f, self.view.frame.size.width, self.view.frame.size.height / 10.5f);
    [_sexView addSubview:femaleButton];
    [femaleButton setTitle:@"Female" forState:UIControlStateNormal];
    [femaleButton addTarget:self action:@selector(handleSelectSex:) forControlEvents:UIControlEventTouchUpInside];
    femaleButton.tag = 200;
    [femaleButton setTitleColor:[UIColor colorWithRed:0.01 green:0.66 blue:0.62 alpha:1.0] forState:UIControlStateNormal];
    
    UIButton *unknowButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_sexView addSubview:unknowButton];
    unknowButton.frame = CGRectMake(0, self.view.frame.size.height * 2 / 10.5f, self.view.frame.size.width, self.view.frame.size.height / 10.5f);
    [unknowButton setTitle:@"I don't know" forState:UIControlStateNormal];
    [unknowButton addTarget:self action:@selector(handleSelectSex:) forControlEvents:UIControlEventTouchUpInside];
    unknowButton.tag = 300;
    [unknowButton setTitleColor:[UIColor colorWithRed:0.01 green:0.66 blue:0.62 alpha:1.0] forState:UIControlStateNormal];
    
    self.view.backgroundColor = [UIColor colorWithRed:0.96 green:0.96 blue:0.96 alpha:1.0];
    [self.view addSubview:_sexView];
}

- (void)handleSelectSex:(UIButton *)button
{
    BmobQuery *query = [BmobQuery queryWithClassName:@"userInfo"];
    [query whereKey:@"userName" equalTo:[EMClient sharedClient].currentUsername];
    [query findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        for (BmobObject *obj in array) {
                    if (button.tag == 100) {
                        [obj setObject:[NSNumber numberWithInteger:1] forKey:@"sex"];
                    }
                    if (button.tag == 200) {
                        [obj setObject:[NSNumber numberWithInteger:0] forKey:@"sex"];
                    }
                    if (button.tag == 300) {
                        [obj setObject:[NSNumber numberWithInteger:3] forKey:@"sex"];
                    }
                    [obj updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                        if (isSuccessful) {
                            NSLog(@"数据更新成功");
                            [self.navigationController popViewControllerAnimated:YES];
                        }
                        else
                        {
                            NSLog(@"%@", error);
                            [self.navigationController popViewControllerAnimated:YES];
                        }
                    }];
           }
    }];
    
}


#pragma mark -
- (void)creatSubViews
{
    self.edit = [[UITextField alloc]init];
    self.edit.text = self.editText;
    [self.view addSubview:self.edit];
    [self.edit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).with.offset(90);
        make.left.equalTo(self.view).with.offset(15);
        make.right.equalTo(self.view).with.offset(-15);
        make.height.mas_equalTo(self.view.frame.size.height / 5.5f);
    }];
    self.edit.layer.borderColor = [[UIColor grayColor]CGColor];
    self.edit.layer.borderWidth = 1.2;
    self.edit.clearsOnBeginEditing = YES;
    [self.edit updateConstraints];
    self.edit.delegate = self;
    self.edit.textAlignment = 4;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleReturn:)];
    [self.view addGestureRecognizer:tap];
    self.view.userInteractionEnabled = YES;
}

- (void)handleReturn:(UITapGestureRecognizer *)tap
{
    [self.view endEditing:YES];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    textField.text = self.editText;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    
    self.navigationItem.rightBarButtonItem.tintColor = [UIColor cyanColor];
}

#pragma mark - 
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
